package cn.z2huo.knowledge.concurrency.lock.locksupport;

import cn.hutool.core.thread.ThreadUtil;

import java.util.concurrent.locks.LockSupport;

/**
 * <p>
 *
 * @author z2huo
 */
class LockSupportDemo {

    static class Worker implements Runnable {

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName()
                    + "，即将进入无限时阻塞");

            LockSupport.park();

            if (Thread.currentThread().isInterrupted()) {
                System.out.println(Thread.currentThread().getName()
                        + "，线程被中断，但仍然会继续执行");
            } else {
                System.out.println(Thread.currentThread().getName() + "，被重新唤醒了");
            }

        }
    }

    public static void main(String[] args) {
        Runnable worker = new Worker();

        Thread thread = new Thread(worker, "thread-1");
        Thread thread2 = new Thread(worker, "thread-2");

        thread.start();
        ThreadUtil.sleep(1000);

        thread2.start();
        ThreadUtil.sleep(1000);

        // 中断线程 1
        thread.interrupt();
        // 唤醒线程 2
        LockSupport.unpark(thread2);
    }

}
